package com.probox.modules.controller.system;

import cn.hutool.core.util.ObjectUtil;

import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.entity.domain.system.SysRegin;
import com.probox.common.entity.service.system.ReginService;
import com.probox.common.security.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 地区Controller
 *
 * @author zsy
 * @date 2021-06-02
 */
@RestController
@RequestMapping("/system/sysregin")
public class ReginController extends BaseController {
    @Autowired
    private ReginService reginService;

    /**
     * 获取地区数据
     */
    @GetMapping("/getRegion")
    public AjaxResult getRegion() {
        return AjaxResult.success(reginService.getRegion());
    }

    /**
     * 获取省市区code
     */
    @PostMapping("/getReginCode")
    public Long[] getReginCode(@RequestBody String[] labels) {
        return reginService.getReginCode(labels);
    }

    /**
     * 搜索省市区
     */
    @PreAuthorize(hasPermi = "base-dict:regionMgr")
    @GetMapping("/queryList")
    public AjaxResult queryList(String name) {
        if (ObjectUtil.isEmpty(name)) {
            return AjaxResult.success(reginService.queryProvince());

        } else {
            return AjaxResult.success(reginService.queryList(name));

        }
    }


    /**
     * 展示省的下级市或市的下级区
     */
    @PreAuthorize(hasPermi = "base-dict:regionMgr")
    @GetMapping("/queryCode")
    public AjaxResult queryList(@RequestParam("type") String type, @RequestParam("code") String code) {
        return AjaxResult.success(reginService.querycode(type, code));
    }


    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SysRegin sysRegin, BindingResult result) {
        reginService.add(sysRegin);
        return AjaxResult.success(true);
    }

    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SysRegin sysRegin, BindingResult result) {
        reginService.edit(sysRegin);
        return AjaxResult.success(true);
    }

    @PutMapping("/del/{uid}/{type}")
    public AjaxResult del(@PathVariable("uid") Long uid, @PathVariable("type") String type) {
        SysRegin sysRegin = new SysRegin();
        sysRegin.setUid(uid);
        sysRegin.setType(type);
        reginService.update(sysRegin);
        return AjaxResult.success(true);
    }

}
